// Node核心模块，用于处理路径
const path = require('path')
// webpack入口模板配置模块
const HtmlWebpackPlugins = require('html-webpack-plugin')
// webpack打包目录清理模块
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
// 应用模块配置
const pagesJs = require('../src/page')
// less数据块打包模块
const ExtractTextPlugin = require("extract-text-webpack-plugin")

// less打包配置
const extractLess = new ExtractTextPlugin({
    filename: "css/[name]_[hash].css",
    disable: process.env.NODE_ENV === "development"
})

let entrys = {}
let htmlPlugins = []
pagesJs.forEach(item => {
    let moduleKey = item.module.replace('/', '_')
    // multi entry configure
    entrys[moduleKey] = path.resolve(__dirname, `../src/controller/${item.module}/index.js`)
    // multi entry view configure
    htmlPlugins.push(new HtmlWebpackPlugins({
        title: item.title,
        favicon: path.resolve(__dirname, '../favicon.ico'),
        filename: `${item.view}.html`,
        template: path.resolve(__dirname, `../src/view/${item.view}.html`),
        chunks: [moduleKey]
    }))
})

// webpack公共配置
module.exports = {
    // 入口脚本
    entry: entrys,
    plugins: [
        new CleanWebpackPlugin(),
        ...htmlPlugins,
        extractLess
    ],
    resolve: {
        // 定义目录快捷别名
        alias: {
            '@': path.join(__dirname, '..', 'src'),
        }
    },
    optimization: {
        // 数据块(公共)分割配置
        splitChunks: {
            cacheGroups: {
                defaultVendors: {
                    filename: 'js/vendor_[hash]_[id].js',
                    chunks: 'initial',
                    minChunks: 2
                }
            }
        }
    },
    // 打包输出配置
    output: {
        filename: "js/[name]_[hash]_[id].js",
        path: path.resolve(__dirname, '../dist')
    },
    // 模块数据转换配置
    module: {
        rules: [
            // js loader es6 into es5
            {
                test: /\.js$/,
                use: 'babel-loader',
                exclude: /node_modules/
            },
            // css loader
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            // ident: 'postcss',
                            plugins: (loader) => [
                                require('postcss-import')({root: loader.resourcePath}),
                                // require('postcss-cssnext')(),
                                require('autoprefixer')(),
                                require('cssnano')()
                            ]
                        }
                    }
                ]
            },
            // less loader
            {
                test: /\.less$/,
                use: extractLess.extract({
                    use: [{
                        loader: "css-loader"
                    }, {
                        loader: "less-loader"
                    }],
                    // use style-loader in development
                    fallback: "style-loader"
                })
            },
            // img loader
            {
                test: /\.(png|svg|jpg|jpeg|gif)(\?.*)?$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            // bytes
                            limit: 8192,
                            name: 'img/[name]_[hash].[ext]'
                        }
                    }
                ]
            },
            // fonts loader
            {
                test: /\.(woff|woff2|eot|ttf|otf)(\?.*)?$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            // bytes
                            limit: 8192,
                            name: 'fonts/[name]_[hash].[ext]'
                        }
                    }
                ]
            }
        ]
    }
}